package com.haikesi.api.role.controller;

import com.github.pagehelper.PageInfo;
import com.haikesi.api.base.ApiIndex;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.R;
import com.haikesi.api.base.Type;
import com.haikesi.api.base.annotation.Token;
import com.haikesi.api.role.domain.vo.*;
import com.haikesi.api.role.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName RoleController
 * @Author HandsomeBugs
 * @Date 2019/4/30 9:32
 * @Version 1.0
 **/

@Api(description = "角色信息相关接口", tags = ApiIndex.RoleController)
@RequestMapping(value = "/api/role")
@RestController
public class RoleController {

    @Autowired
    private IRoleService service;


    @ApiOperation(value = "查询列表")
    @GetMapping(value = "/list")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<PageInfo<List<RoleVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute RoleSearchVO searchVO) {
        List<RoleVO> list = service.getList(searchVO);
        PageInfo pageInfo = new PageInfo(list);
        return new R(pageInfo);
    }



    @ApiOperation(value = "创建")
    @PostMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R create(
            @ApiParam(value = "添加参数") @Valid @RequestBody RoleSaveVO saveVO,BindingResult bindingResult) {
        if(bindingResult.hasErrors()) {
            List<Error> messages=new ArrayList<>();
            for(int i=0;i<bindingResult.getFieldErrors().size();i++){
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(),bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        return new R().setData(service.create(saveVO));
    }


    @ApiOperation(value = "修改")
    @PutMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R update(
            @ApiParam(value = "修改参数") @Valid @RequestBody RoleUpdateVO updateVO,BindingResult bindingResult) {
        if(bindingResult.hasErrors()) {
            List<Error> messages=new ArrayList<>();
            for(int i=0;i<bindingResult.getFieldErrors().size();i++){
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(),bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        service.update(updateVO);
        return new R();
    }



    @ApiOperation(value = "删除")
    @DeleteMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R delete(
            @ApiParam(value = "修改参数") @RequestParam String id) {
        service.delete(id);
        return new R();
    }

    @ApiOperation(value = "角色赋权")
    @PostMapping(value = "addAuthority")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R create(
            @ApiParam(value = "参数") @Valid @RequestBody RoleAuthorityVO RoleAuthorityVO, BindingResult bindingResult) {
        if(bindingResult.hasErrors()) {
            List<Error> messages=new ArrayList<>();
            for(int i=0;i<bindingResult.getFieldErrors().size();i++){
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(),bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        service.addAuthority(RoleAuthorityVO);
        return new R();
    }


    @ApiOperation(value = "查询角色权限")
    @GetMapping(value = "getAuthority")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<List<String>> create(
            @ApiParam(value = "角色ID") @RequestParam String roleId) {
        if(StringUtils.isBlank(roleId)){
            return new R().set(Type.PARAM_VALIDATE_FAIL);
        }
        return new R().setData(service.getAuthority(roleId));
    }

}
